<?php
namespace api\modules\merchant\controllers\member;

use Yii;
use api\modules\merchant\models\ResetPasswordForm;
use yii\web\NotFoundHttpException;
use api\controllers\OnAuthController;
use common\enums\StatusEnum;
use common\helpers\ResultDataHelper;


/**
 * 会员接口
 *
 * Class InfoController
 * @package api\modules\merchant\controllers\member
 * @property \yii\db\ActiveRecord $modelClass
 * @author jianyan74 <751393839@qq.com>
 */
class InfoController extends OnAuthController
{
    public $modelClass = 'common\models\merchant\Merchant';

    /**
     * @param $id
     * @return int|mixed
     * @throws \yii\base\Exception
     * @throws \yii\web\UnprocessableEntityHttpException
     */
    public function actionProfile($id){
        if (Yii::$app->user->identity->member_id != $id)
        {
            throw new NotFoundHttpException('权限不足.');
        }

        try
        {
            $model = new ResetPasswordForm($id);
        }
        catch (UnprocessableEntityHttpException $e)
        {
            throw new BadRequestHttpException($e->getMessage());
        }
        $model->attributes = Yii::$app->request->post();
        if ( $model->validate() && $model->resetPassword())
        {
            return [];
        }
        return ResultDataHelper::api(422, $this->analyErr($model->getErrors()));
    }

    /**
     * 修改用户信息
     * @param $id
     * @return array|mixed|\yii\db\ActiveRecord|null
     * @throws NotFoundHttpException
     */
    public function actionUpdate($id)
    {
        if (Yii::$app->user->identity->member_id != $id)
        {
            throw new NotFoundHttpException('权限不足.');
        }
        $post = Yii::$app->request->post();
        foreach ($post as $k=>$v){
            if(!in_array($k, ['nickname','realname','head_portrait','gender','qq','birthday','home_phone','mobile','provinces','city','area','domain'])){
                unset($post[$k]);
            }
        }
        $model = $this->modelClass::findOne($id);
        $model->attributes = $post;
        if (!$model->save())
        {
            return ResultDataHelper::api(422, $this->analyErr($model->getFirstErrors()));
        }

        $model = $this->modelClass::find()
            ->where(['id' => $id, 'status' => StatusEnum::ENABLED])
            ->select(['id','type', 'username', 'nickname', 'realname', 'head_portrait', 'gender', 'qq', 'email', 'birthday','domain', 'user_money', 'accumulate_money','frozen_money','user_integral', 'visit_count','home_phone','mobile','role','last_time','last_ip','provinces','city','area','status', 'created_at','updated_at'])
            ->asArray()
            ->one();

        if (!$model)
        {
            throw new NotFoundHttpException('请求的数据不存在或您的权限不足.');
        }

        return $model;
    }

    /**
     * 单个显示
     *
     * @param $id
     * @return mixed
     * @throws NotFoundHttpException
     */
    public function actionView($id)
    {
        $model = $this->modelClass::find()
            ->where(['id' => $id, 'status' => StatusEnum::ENABLED])
            ->select(['id','type', 'username', 'nickname', 'realname', 'head_portrait', 'gender', 'qq', 'email', 'birthday','domain', 'user_money', 'accumulate_money','frozen_money','user_integral', 'visit_count','home_phone','mobile','role','last_time','last_ip','provinces','city','area','status', 'created_at','updated_at'])
            ->asArray()
            ->one();

        if (!$model)
        {
            throw new NotFoundHttpException('请求的数据不存在或您的权限不足.');
        }

        return $model;
    }

    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
        if (in_array($action, ['delete', 'index']))
        {
            throw new \yii\web\BadRequestHttpException('权限不足');
        }
    }
}
